define(function(require, exports, module) {
	//lib工具包
	require('resources/js/jquery.json-2.4.min.js');
	require('avalon2');
	
	var ZTreeMenu=require('resources/js/component/ZTreeMenu.js');
	var OpenGridList=require('resources/template/common/OpenGridList.js');
	var resConstants=require('resources/js/rescatalogMix/res_constants.js');
	
	var ResInfoMain=function(params){
		//配置
		$.extend(true,this,params);
		this.init();
	};
	
	ResInfoMain.prototype={
		urls:{
			catalogMenu:'/catalogType/treeJson.jhtml?id=1',
			list:'/resInfoMain/list.jhtml',
			viewCreate:'/resInfo/create.jhtml',
			search:'/resInfo/main.jhtml'
		},
		query:function(){
			//>查询列表条件：分类, 资源名称。 
			this.gridList.load({
				bizName:this.vm.bizName,
				catalogId:this.catalogMenu.getCurItemId(),
				pageNo:1
			})
		},
		getPageParam:function(){
			return $.extend(this.gridList.getParam(),{
				preName:'资源查询',
				preUrl:this.getUrl('search')
			});
		},
		init:function(){
			this.render();
			this.bind();
			this.sync();
			
			var that=this;
			//分类树,  分类菜单
			this.catalogMenu=new ZTreeMenu($('#catalogMenu'),{
				menuId:'catalogMenu',
				dataUrl:this.getUrl('catalogMenu'),
				curItem:this.listParam.catalogId,
				onNodeClick:function(treeId, treeNode){
					that.query();
				}
			});
			//列表(含分页)  
			this.gridList=new OpenGridList($('.docs-list-box'),{
				//查看资源
				data:{
					param:{
						bizName:this.listParam.bizName,
						catalogId:this.listParam.catalogId
					}
				},
				dataUrl:this.getUrl('list'),
				keyColumn:'bizName',
				columns:{ //'code':{title:'',render:function(value){}}
					'bizName':{title:'资源名称',width:'30%'},
					'resType':{title:'资源类型',width:'15%',render:function(value,record){
						return resConstants.resType[value];
					}},
					'resProviderName':{title:'提供部门',width:'17%'},
					'createUserName':{title:'创建者',width:'13%'},
					'createTime':{title:'创建时间',width:'15%'}
				},
				onClickRecord:function(){}
			});
		},
		render:function(){
			
		},
		operateSelectValidate:function(){
			var selectedRecord=this.gridList.getSelectedRecords(),
				other={},otherNames=[];
			if(selectedRecord.length==0)
			{
				tip('至少选择一个资源!');
				return false;
			}
			for(var i=0;i<selectedRecord.length;i++)
				if(selectedRecord[i]['createUserId']!=this.userId)
					other[selectedRecord[i]['createUserName']]=1;
			for(i in other)
				otherNames.push(i);
			if(otherNames.length>0)
			{
				tip('不能操作其他人的资源:'+otherNames.join(','));
				return false;
			}	
		},
		bind:function(){
			var that=this;
			$('#btnCreate').on('click',function(){//新建
				that.jumpView('viewCreate');
			});
			$('#btnEdit').on('click',function(){// 修改资源
				if(!that.operateSelectValidate())
					return;
			});
			$('#btnDel').on('click',function(){// 删除资源 , 
				if(!that.operateSelectValidate())
					return;
			});
			$('#btnApply').on('click',function(){//申请
				
			});
			$('#btnSearch').on('click',function(){//查询
				that.query();
			});
		},
		sync:function(){
			this.vm=avalon.define({
				$id: "resInfoMainVM",
				curRecord:{},
				bizName:this.listParam.bizName
			});
			avalon.scan($('#console-box').get(0),this.vm);
		},
		getUrl:function(action){
			if(this.urls[action])
				return this.baseUrl+this.urls[action];
			else 
				return this.baseUrl+action;
		},
		jumpView:function(action,param){
			jump(this.baseUrl+this.urls[action],$.extend(this.getPageParam(),param));
		}
	};
	
	return ResInfoMain;
});	